package com.kpx.app.utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.alibaba.sdk.android.oss.model.ResumableUploadRequest;
import com.alibaba.sdk.android.oss.model.ResumableUploadResult;
import com.kpx.app.listener.AliyunUploadListener;

import java.io.FileNotFoundException;

/**
 * 作者： Ryan
 * 描述：
 * 创建时间：2015/4/7 15:31.
 */
public class AliyunFileUploader {
    private String sourceFilePath;
    private String fileCloudKey;
    private String bucketName;
    private Context activity;
    private AliyunUploadListener listener;

    public AliyunFileUploader(String sourceFilePath, String fileCloudKey, String bucketName, Context activity, AliyunUploadListener listener) {
        this.sourceFilePath = sourceFilePath;
        this.fileCloudKey = fileCloudKey;
        this.bucketName = bucketName;
        this.activity = activity;
        this.listener = listener;
    }

    public OSSAsyncTask upload() throws FileNotFoundException {
        return AliyunOSSUtils.getOssUtils().uploadFileToOSS(bucketName, fileCloudKey, sourceFilePath, new OSSProgressCallback() {
            @Override
            public void onProgress(Object o, final long currentSize, final long totalSize) {
                if (activity != null) {
                    if (activity instanceof Activity && !((Activity) activity).isFinishing()) {
                        ((Activity) activity).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (listener != null) {
                                    listener.onProgress(fileCloudKey, currentSize, totalSize);
                                }
                            }
                        });
                    } else {
                        if (listener != null) {
                            listener.onProgress(fileCloudKey, currentSize, totalSize);
                        }
                    }
                }
            }

        }, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                Log.d("resumableUpload", "success!");
                if (activity != null) {
                    if (activity instanceof Activity && !((Activity) activity).isFinishing()) {
                        ((Activity) activity).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (listener != null) {
                                    listener.onSuccess(fileCloudKey, sourceFilePath);
                                }
                            }
                        });
                    } else {
                        if (listener != null) {
                            listener.onSuccess(fileCloudKey, sourceFilePath);
                        }
                    }
                }
            }
            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, final ServiceException serviceException) {
                if (clientExcepion != null) {
                    // 本地异常如网络异常等
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    // 服务异常
                    Log.e("ErrorCode", serviceException.getErrorCode());
                    Log.e("RequestId", serviceException.getRequestId());
                    Log.e("HostId", serviceException.getHostId());
                    Log.e("RawMessage", serviceException.getRawMessage());
                }
                if (activity != null) {
                    if (activity instanceof Activity && !((Activity) activity).isFinishing()) {
                        ((Activity) activity).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (listener != null) {
                                    listener.onFailure(fileCloudKey, serviceException);
                                }
                            }
                        });
                    }else{
                        if (listener != null) {
                            listener.onFailure(fileCloudKey, serviceException);
                        }
                    }
                }
            }
        });
    }

    /**
     * 断点续传
     *
     * @throws FileNotFoundException
     */
    public OSSAsyncTask uploadResume() throws FileNotFoundException {
        return AliyunOSSUtils.getOssUtils().resumableUpload(bucketName, fileCloudKey, sourceFilePath, new OSSProgressCallback() {
            @Override
            public void onProgress(Object o, final long currentSize, final long totalSize) {
                if (activity != null) {
                    if (activity instanceof Activity && !((Activity) activity).isFinishing()) {
                        ((Activity) activity).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (listener != null) {
                                    listener.onProgress(fileCloudKey, currentSize, totalSize);
                                }
                            }
                        });
                    }else{
                        if (listener != null) {
                            listener.onProgress(fileCloudKey, currentSize, totalSize);
                        }
                    }
                }
            }

        }, new OSSCompletedCallback<ResumableUploadRequest, ResumableUploadResult>() {
            @Override
            public void onSuccess(ResumableUploadRequest request, ResumableUploadResult result) {
                Log.d("resumableUpload", "success!");
                if (activity != null) {
                    if (activity instanceof Activity && !((Activity) activity).isFinishing()) {
                        ((Activity) activity).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (listener != null) {
                                    listener.onSuccess(fileCloudKey, sourceFilePath);
                                }
                            }
                        });
                    }else{
                        if (listener != null) {
                            listener.onSuccess(fileCloudKey, sourceFilePath);
                        }
                    }
                }
            }

            @Override
            public void onFailure(ResumableUploadRequest request, ClientException clientExcepion, final ServiceException serviceException) {
                // 异常处理
                // 请求异常
                if (clientExcepion != null) {
                    // 本地异常如网络异常等
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    // 服务异常
                    Log.e("ErrorCode", serviceException.getErrorCode());
                    Log.e("RequestId", serviceException.getRequestId());
                    Log.e("HostId", serviceException.getHostId());
                    Log.e("RawMessage", serviceException.getRawMessage());
                }
                if (activity != null) {
                    if (activity instanceof Activity && !((Activity) activity).isFinishing()) {
                        ((Activity) activity).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (listener != null) {
                                    listener.onFailure(fileCloudKey, serviceException);
                                }
                            }
                        });
                    }else{
                        if (listener != null) {
                            listener.onFailure(fileCloudKey, serviceException);
                        }
                    }
                }
            }
        });
    }

}
